﻿using NoDriverPrintHelperV1d0.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using ZhangLogSysV1d0;

namespace NoDriverPrintHelperV1d0.Service
{
    /// <summary>
    /// 该类暂不使用
    /// </summary>
    public class ThreadPrintTaskDispatchV1d0
    {
        /// <summary>
        ///打印任务开始打印事件
        /// </summary>
        public event EventHandler<long> PrintTaskBeginingEvent = null;
        ///// <summary>
        ///// 射频卡数据事件
        ///// </summary>
        //public event EventHandler<RfidDataV1d0> RfidDataEvent = null;

        /// <summary>
        /// 是否运行中
        /// </summary>
        private bool runing = false;
        /// <summary>
        /// epc扫描线程
        /// </summary>
        private Thread Thread_printTask = null;
        /// <summary>
        /// 线程序号
        /// </summary>
        public int Thread_index = 0;

        /// <summary>
        /// 当前正在处理的任务序号
        /// </summary>
        public long CurrProcessTaskIndex = 0;
        /// <summary>
        /// 已处理完成的任务序号
        /// </summary>
        public int ProcessedTaskIndex = -1;

        /// <summary>
        /// 已接收任务列表
        /// </summary>
        public List<PrintTaskV1d0> ReceivedTaskList = new List<PrintTaskV1d0>();
        public Dictionary<Guid, int> ReceivedTaskDict = new Dictionary<Guid, int>();
        /// <summary>
        /// 打印完成任务列表
        /// </summary>
        public List<PrintTaskV1d0> PrintCompleteTaskList = new List<PrintTaskV1d0>();
        /// <summary>
        /// 打印错误任务列表
        /// </summary>
        public List<PrintTaskV1d0> PrintErrorTaskList = new List<PrintTaskV1d0>();
        public Dictionary<Guid, int> PrintErrorTaskDict = new Dictionary<Guid, int>();


        /// <summary>
        /// 启动线程
        /// </summary>
        public void StartThread()
        {
            this.Thread_printTask = new Thread(new System.Threading.ThreadStart(this.ThreadStart));
            this.runing = true;
            //作为后台线程
            this.Thread_printTask.IsBackground = false;
            //启动线程
            this.Thread_printTask.Start();
        }
        /// <summary>
        /// 停止线程
        /// </summary>
        public void StopThread()
        {
            this.runing = false;
        }

        /// <summary>
        /// 线程启动
        /// </summary>
        private void ThreadStart()
        {
            while (runing)
            {
                try
                {
                    if(ReceivedTaskList.Count>0)
                    {
                        //Guid toBeProcessTaskGuid= ReceivedTaskDict.get
                        var toBeProcessTaskList = ReceivedTaskList.FindAll(x => x.task_index >= CurrProcessTaskIndex);
                        //得到task_index最小的值，作为CurrProcessTaskIndex 当前处理任务的任务序号
                        CurrProcessTaskIndex = toBeProcessTaskList.Min(x => x.task_index);
                        //处理该打印任务
                        ProcessPrintTask(CurrProcessTaskIndex);
                    }
                    else if(PrintErrorTaskList.Count>0)
                    {

                    }

                    //Thread.Sleep(200);
                }
                catch (Exception ex)
                {
                    SimpleLoger.Instance.Error(ex);
                    continue;
                }
            }
        }

        private void ProcessPrintTask(long  taskIndex)
        {

        }

       
    }
}
